package com.sixth.volunteerservicesystem.Controller.DonationController.backDonation;

import com.sixth.volunteerservicesystem.Dao.DonationRepository;
import com.sixth.volunteerservicesystem.domain.DonationProjects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Steve Jobs
 * @create 2021-01-11 17:15
 */
@Controller
public class UpdateDonation {

    @Autowired
    DonationRepository donationRepository;

    @RequestMapping("/updateDonation/{id}")
    public String updateDonation(@PathVariable("id") Integer id, Model model){

        DonationProjects donationProjects = new DonationProjects();

       donationProjects =  donationRepository.findById(id).get();


        model.addAttribute("donation", donationProjects);

        return "backstageDonation/updateDonation";
    }

    @PostMapping("/updateDonationBack/{id}")
    public String updateDonationBack(@RequestParam("title") String title,
                                  @RequestParam("content") String content,
                                  @RequestParam("process") String process,
                                  @RequestParam("src") String src,
                                     @PathVariable("id") Integer id
    ){

        src = "/donationImg/" + src;

        DonationProjects donationProjects = new DonationProjects();
        donationProjects.setProductId(id);
        donationProjects.setProductTitle(title);
        donationProjects.setContent(content);
        donationProjects.setProgress(process);
        donationProjects.setSrc(src);

        donationRepository.save(donationProjects);

        return "redirect:/backDonationList";
    }

}
